package com.zretc.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zretc.user.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping(value="/quick22")
    @ResponseBody
    public void save22(String username, MultipartFile uploadFile) throws IOException {
        System.out.println(username);
        //获得上传文件的名称
        String originalFilename = uploadFile.getOriginalFilename();
        uploadFile.transferTo(new File("C:\\Users\\MI\\Desktop\\upload\\"+originalFilename));
    }


    @RequestMapping("/quick20")
    @ResponseBody
    public void save20(@RequestHeader( value = "user-agent",required = false) String user_agent) {
        System.out.println(user_agent);
    }

    @RequestMapping("/quick18")
    @ResponseBody
    public void save18(Date date) {
        System.out.println(date);
    }


    @RequestMapping("/quick17/{name}")
    @ResponseBody
    public void save817( @PathVariable("name") String username) {
        System.out.println(username);
    }


    @RequestMapping("/quick16")
    @ResponseBody
    public void save8( @RequestParam("name") String username) {
        System.out.println(username);
    }


    @RequestMapping("/quick8")
    @ResponseBody
    public String save8() throws JsonProcessingException {
        User user = new User();
        user.setName("zhangsna");
        user.setAge(20);

        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(user);

        return s;
    }


    @RequestMapping("/quick7")
    @ResponseBody
    public String save7(HttpServletResponse response) throws IOException {
        return "nihaoa548458418548";
    }

    @RequestMapping("/quick6")
    public void save6(HttpServletResponse response) throws IOException {
        response.getWriter().write("<h1>nihao</h1>");
    }

    @RequestMapping("/quick4")
    public String save4(Model mode) {
        mode.addAttribute("username", "睡眠日");
        return "success";
    }

    @RequestMapping("/quick3")
    public ModelAndView save3(ModelAndView modelAndView) {
        modelAndView.addObject("username", "hello33");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/quick2")
    public ModelAndView save2() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("username", "hello");
        modelAndView.setViewName("success");
        return modelAndView;
    }


    @RequestMapping("/quick")
    public String save() {
        System.out.println("success");
        return "success";
    }

}
